const { ccclass, property } = cc._decorator;

@ccclass
export default class TouchScale extends cc.Component {
    @property({ type: cc.Node, displayName: "遮照节点" })
    maskNode: cc.Node = null;
    @property({ type: cc.Float, displayName: "移动速度" })
    touchSpeed: number = 1;
    @property({ type: cc.Float, displayName: "双指缩放速度" })
    scaleNum: number = 0.001;


    @property({ type: cc.Float, displayName: "最小缩放值" })
    minScale: number = 1.1;
    @property({ type: cc.Float, displayName: "最大缩放值" })
    maxScale: number = 5;

    /** 是否自动缩小 */
    @property(cc.Boolean)
    isAutoScale: boolean = true;
    /** 是否只能横屏拖动 */
    @property(cc.Boolean)
    isHorTouch: boolean = false;
    /** 是否开启缩放 */
    @property(cc.Boolean)
    isTouchScale: boolean = true;

    lastDisc: number = 0;
    // LIFE-CYCLE CALLBACKS:
    // onLoad () {}
    start() {
        if (!this.isAutoScale) {
            return;
        }
        this.scheduleOnce(() => {
            this.updateMinScale();
            this.node.scale = this.minScale;
            this.node.position = cc.v3(0, this.node.y);
        }, 0.1);
        // cc.log(this.minScale,this.node.width,this.maskNode.width,this.node.height,this.maskNode.height)
    }
    // update (dt) {}
    /** 鼠标滚轮 */
    mouseWheelCall(touches: cc.Event.EventMouse) {
        let moveLen = touches.getScrollY();
        this.updateMapScale(moveLen)
    };

    /** 触摸移动 */
    touchMoveCall(event: cc.Event.EventTouch) {

        let delta = event.getDelta();
        let touches = event.getTouches();
        // cc.log(touches.length)
        if (touches.length == 1) {
            let tempX = delta.x * this.touchSpeed;
            let tempY = delta.y * this.touchSpeed;
            if(this.isHorTouch){
                tempY = 0;
            }
            // let tempPos = cc.v3(this.node.position.x + tempX, this.node.position.y + tempY, 0)
            // this.node.position = cc.v3(tempPos.x, tempPos.y, 0);
            this.setResictPos(tempX, tempY);
        } else if (touches.length == 2) {
            // cc.log("sssssssssssss")
            let disc = cc.Vec2.distance(touches[0].getLocation(), touches[1].getLocation());
            if (this.lastDisc == 0) {
                this.lastDisc = disc;
                return;
            }
            // let tempX = touches[0].getUILocationX() - window.win_Width / 2 + (touches[1].getLocationX() - touches[0].getLocationX());
            // let tempY = touches[0].getUILocationY() - window.win_Height / 2 + (touches[1].getLocationY() - touches[0].getLocationY());

            let tempLen = (disc - this.lastDisc);
            this.updateMapScale(tempLen);
            this.lastDisc = disc;
        }


    };
    touchMoveEnd(event: cc.Event.EventTouch) {
        this.lastDisc = 0;
    };

    /** 刷新大小  tempLen变化多少 */
    updateMapScale(tempLen: number) {
        if(!this.isTouchScale){ return; }
        let tempScale = this.node.scale + tempLen * this.scaleNum;
        if (tempScale <= this.minScale) {
            tempScale = this.minScale;
        } if (tempScale >= this.maxScale) {
            tempScale = this.maxScale;
        }
        if (tempScale == this.minScale) {
            this.lastDisc = 0;
            this.node.scale = tempScale;
            this.node.position = cc.v3(0, 0);
            return;
        }
        let width = this.node.width * this.node.scaleX;
        let height = this.node.height * this.node.scaleY;

        let tempAnchorX = 1 + (this.node.position.x - width / 2) / width;
        let tempAnchorY = 1 + (this.node.position.y - height / 2) / height;
        let tempX = this.node.width * tempScale * (tempAnchorX - 0.5);
        let tempY = this.node.height * tempScale * (tempAnchorY - 0.5);
        this.node.position = cc.v3(tempX, tempY, 0);
        this.node.scale = tempScale;

        this.setResictPos();
    };
    /** 超出界限重置 */
    setResictPos(tempX: number = 0, tempY: number = 0) {
        if (this.node.scale == this.minScale && this.isAutoScale) {
            this.node.position = cc.v3(0, 0);
            this.lastDisc = 0;
            return;
        }

        let width = this.node.width * this.node.scaleX;
        let height = this.node.height * this.node.scaleY;
        let toPos = cc.v3(this.node.x + tempX, this.node.y + tempY);
        if (width >= this.maskNode.width) {
            if (toPos.x - width / 2 > -this.maskNode.width / 2) {
                toPos.x = -this.maskNode.width / 2 + width / 2;
            } if (toPos.x + width / 2 < this.maskNode.width / 2) {
                toPos.x = this.maskNode.width / 2 - width / 2;
            }
        } else {
            if (toPos.x - width / 2 < -this.maskNode.width / 2) {
                toPos.x = -this.maskNode.width / 2 + width / 2;
            } if (toPos.x + width / 2 > this.maskNode.width / 2) {
                toPos.x = this.maskNode.width / 2 - width / 2;
            }
        }

        if (height >= this.maskNode.height) {
            if (toPos.y - height / 2 > -this.maskNode.height / 2) {
                toPos.y = -this.maskNode.height / 2 + height / 2;
            } if (toPos.y + height / 2 < this.maskNode.height / 2) {
                toPos.y = this.maskNode.height / 2 - height / 2;
            }
        } else {
            if (toPos.y - height / 2 < -this.maskNode.height / 2) {
                toPos.y = -this.maskNode.height / 2 + height / 2;
            } if (toPos.y + height / 2 > this.maskNode.height / 2) {
                toPos.y = this.maskNode.height / 2 - height / 2;
            }
        }

        // if (toPos.x - width / 2 < -this.maskNode.width/2 ) {
        //     toPos.x = -this.maskNode.width/2 + width / 2;
        // } if (toPos.x + width / 2 > this.maskNode.width/2) {
        //     toPos.x = this.maskNode.width/2 - width / 2;
        // } if (toPos.y - height / 2 <  -this.maskNode.height/2 ) {
        //     toPos.y = -this.maskNode.height / 2 + height / 2;
        // } if (toPos.y + height / 2 > this.maskNode.height / 2) {
        //     toPos.y = this.maskNode.height / 2 - height / 2;
        // }
        this.node.position = toPos;
    };

    updateMinScale(){
        let minScale = this.maskNode.width / this.node.width;
        let minScale2 = this.maskNode.height / this.node.height;
        this.minScale = Math.min(minScale, minScale2);
    }
    // /** 放大缩小是否达到最小值 */
    // setScaleBound() {
    // }
    protected onDestroy(): void {
        this.unscheduleAllCallbacks();
    }
}
